package com.dongjiantong.web.boss.controller;/**
 * Created by cao on 2017/12/1.
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.common.utils.IntegerUtil;
import com.dongjiantong.common.utils.StringUtil;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.dongjiantong.sys.model.DicData;
import com.dongjiantong.sys.model.DicIllness;
import com.dongjiantong.sys.model.DicUnit;
import com.dongjiantong.sys.model.SysLabelModel;
import com.dongjiantong.sys.service.DicDataService;
import com.dongjiantong.sys.service.DicIllnessService;
import com.dongjiantong.sys.service.DicUnitService;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Optional;
import com.sun.org.apache.regexp.internal.RE;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 字典及基础数据查看或管理
 *
 * @author cao
 * @create 2017-12-01 下午5:43
 **/
//@RestController
//@RequestMapping("/api/boss/dic")
@Slf4j
@OpenBean
public class DicController {

    @Reference(version = "1.0.0")
    public DicUnitService dicUnitService;

    @Reference(version = "1.0.0")
    public DicIllnessService dicIllnessService;

    @Reference(version = "1.0.0")
    public DicDataService dicDataService;


    /**
     * 查询系统单位
     *
     * @return
     */
    //@GetMapping("/queryUnitList")
    @OpenMethod(

            key = "boss.dic.queryUnitList",
            paramNames = {"pageNum", "pageSize", "code"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<DicUnit>> queryUnitList(Integer pageNum, Integer pageSize, Integer code) {
        PageInfo pageInfo = dicUnitService.selectPage(pageNum, pageSize, new DicUnit());
        return Response.ok(pageInfo);
    }


    /**
     * 保存单位
     *
     * @param dicUnit
     * @return
     */
    //@PostMapping("/saveUnit")
    @OpenMethod(
            key = "boss.dic.saveUnit",
            paramNames = {"dicUnit"},
            httpMethods = RequestMethod.POST
    )
    public Response<String> saveUnit(DicUnit dicUnit) {
        if (dicUnit.getAnimalType() == null) {
            return Response.fail(HttpCode.BAD_REQUEST, "畜种不能为null,如果是通用单位请传0");
        }
        if (dicUnit.getCode() == null) {
            return Response.fail(HttpCode.BAD_REQUEST, "请输入code");
        }
        if (StringUtil.isBlank(dicUnit.getUnit())) {
            return Response.fail(HttpCode.BAD_REQUEST, "请输入单位名称");
        }
        try {
            dicUnitService.insert(dicUnit);
            return Response.ok("创建成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return Response.fail("创建失败");
        }


    }

    /**
     * 查询病种
     *
     * @return
     */

    @OpenMethod(
            key = "boss.dic.queryIllnessList",
            paramNames = {"pageNum","pageSize"},
            httpMethods = RequestMethod.GET
    )
    public Response<PageInfo<DicIllness>> queryIllnessList(Integer pageNum, Integer pageSize) {
        return Response.ok(dicIllnessService.selectPage(pageNum, pageSize, new DicIllness()));
    }


    /**
     * 保存病种
     *
     * @return
     */
    //@PostMapping("/saveIllness")
    @OpenMethod(
            key = "boss.dic.saveIllness",
            paramNames = {"dicIllness"},
            httpMethods = RequestMethod.POST
    )
    public Response<String> saveIllness(DicIllness dicIllness) {
        if (dicIllness.getAnimalType() == null) {
            return Response.fail(HttpCode.BAD_REQUEST, "畜种不能为null");
        }
        if (dicIllness.getIllcode() == null) {
            return Response.fail(HttpCode.BAD_REQUEST, "请输入code");
        }
        if (StringUtil.isBlank(dicIllness.getIllname())) {
            return Response.fail(HttpCode.BAD_REQUEST, "请输入病种名称");
        }

        try {
            dicIllnessService.insert(dicIllness);
            return Response.ok("创建成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return Response.fail("创建失败");
        }

    }

    //////////////////数据字典表///////////////////////////////

    /**
     * 企业类型
     *
     * @return
     */
    //@GetMapping("/queryFarmsType")
    @OpenMethod(
            key = "boss.dic.queryFarmsType",
            paramNames = {},
            httpMethods = RequestMethod.GET
    )
    public Response<List<DicData>> queryFarmsType() {
        return Response.ok(dicDataService.queryFarmsType());
    }

    /**
     * 取APP角色（详细）
     *
     * @return
     */
    //@GetMapping("/queryAPPRole")
    @OpenMethod(
            key = "boss.dic.queryAPPRole",
            paramNames = {},
            httpMethods = RequestMethod.GET
    )
    public Response<List<DicData>> queryAPPRole() {
        return Response.ok(dicDataService.queryAPPRole());
    }


    /**
     * 获取APP角色  value  lable
     *
     * @return
     */

    @OpenMethod(
            key = "boss.sys.selectAPPRole",
            httpMethods = RequestMethod.GET
    )
    public Response<List> selectAPPRole() {
        List<DicData> list = dicDataService.queryAPPRole();
        List<SysLabelModel> sysLabelModels = new ArrayList<>();
        for (DicData dicData : list) {
            if (Integer.parseInt(dicData.getDicValue()) < 2009) {
                SysLabelModel sysLabelModel = new SysLabelModel();
                sysLabelModel.setValue(Integer.valueOf(dicData.getDicValue()));
                sysLabelModel.setLabel(dicData.getDicName());
                sysLabelModels.add(sysLabelModel);
            }

        }

        return Response.ok(sysLabelModels);
    }

}
